/* 
 **axios服务配置文件
 */
 import axios from 'axios';
 import { Message } from 'element-ui';
 
 // 创建实例
 const service = axios.create({
   baseURL: '', //process.env.BASE_API, // 使用代理
   timeout: 30000 // 请求超时时间
 });
 
 // request拦截器
 service.interceptors.request.use(
   config => {
     if (sessionStorage.getItem('token')) {
       config.headers.token = sessionStorage.getItem('token');
     }
     return config;
   },
   error => {
     return Promise.reject(error);
   }
 );
 
 // response拦截器
 service.interceptors.response.use(
   response => response,
   error => {
     return Promise.reject(error);
   }
 );
 
 // 用一个新的promise再包一层处理抛出的错误
 const fetch = function(config) {
   return new Promise((resolve, reject) => {
     service(config).then(res => {
       let data = res.data;
       if (data.code === 0) {
         resolve(data);
       }else {
        Message({
					message: data.msg,
					type: 'warning'
				});
        resolve(data);
       }
     }).catch(res => {
       reject(res);
     })
   })
 };
 export default fetch;
 
 export const fetchPost = (url, data) => {
  // url = url + '?_r' + new Date().getTime();
   return fetch({
     method: 'post',
     url: url,
     headers: {
       'Content-type': 'application/json;chartset=utf-8' //'application/x-www-form-urlencoded',
     },
     data: data
   })
 }
 
 export const fetchGet = (url, params) => {
   //url = url + '?_r=' + new Date().getTime();
   return fetch({
     method: 'get',
     url: url,
     headers: {
       'Content-type': 'application/x-www-form-urlencoded'
     },
     params: params
   })
 }